package billing.image.utils;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import billing.image.bean.ImageBean;

public class BufferedImageUtils {

	public static byte[] toByteArray(ImageBean src) throws IOException{
		byte[] result = null;
		ByteArrayOutputStream out = new ByteArrayOutputStream();  
        boolean flag = ImageIO.write(src.getBufferedImage(),src.getType(), out);  
        if(flag){
        	  result = out.toByteArray();  
        }
		return result;
	}
	
	
	public static ImageBean toImageBean(byte[] src) throws IOException{
		ImageBean result = null;
		ByteArrayInputStream bin = new ByteArrayInputStream(src);  
	    BufferedImage image = ImageIO.read(bin);  
	    result = new ImageBean(image);
		return result;
	}
	
}
